package com.examine.action;

import com.alibaba.fastjson.JSON;
import com.examine.bean.Message;
import com.examine.bean.News;
import com.examine.bean.PageResult;
import com.examine.serviceImpl.MessageServiceImpl;
import com.examine.serviceImpl.NewsServiceImpl;
import com.examine.utils.DateUtil;
import com.examine.utils.StrResponse;
import com.examine.utils.UuidUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;

public class MessageJsonAction extends ActionSupport {
    private LinkedHashMap mymp;
    private MessageServiceImpl messageService;
    private Message message;

    public LinkedHashMap getModelMap() {
        return mymp;
    }
    public void setModelMap(LinkedHashMap mymp) {
        this.mymp = mymp;
    }


    public String addMessage(){
        message = JSON.parseObject(StrResponse.getStrResponse(), Message.class);

        messageService = new MessageServiceImpl();

        message.setId(UuidUtil.getUUID());
        message.setCreateTime(DateUtil.getCurrentTime());

        int count = messageService.insertSelective(message);
        if (count == 0){
            mymp = StrResponse.error("添加消息失败");
            return ERROR;
        }

        mymp = StrResponse.success("添加消息成功");
        return SUCCESS;
    }

    public String updateMessage(){
        message = JSON.parseObject(StrResponse.getStrResponse(), Message.class);

        messageService = new MessageServiceImpl();

        message.setUpdateTime(DateUtil.getCurrentTime());

        int count = messageService.updateByPrimaryKeySelective(message);
        if (count == 0){
            mymp = StrResponse.error("修改消息失败");
            return ERROR;
        }

        mymp = StrResponse.success("修改消息成功");
        return SUCCESS;
    }

    public String selectStripMessage(){
        message = JSON.parseObject(StrResponse.getStrResponse(), Message.class);

        if(StringUtils.isEmpty(message.getId())){
            mymp = StrResponse.error("消息ID不能为空");
            return ERROR;
        }

        messageService = new MessageServiceImpl();

        Message message1 = messageService.selectByPrimaryKey(message.getId());
        if (message1 == null){
            mymp = StrResponse.error("查询消息失败");
            return ERROR;
        }

        mymp = StrResponse.success("查询消息成功", message1);
        return SUCCESS;
    }


    public String selectMessageAll(){
        message = JSON.parseObject(StrResponse.getStrResponse(), Message.class);

        messageService = new MessageServiceImpl();

        List<Message> messageList = messageService.getAllMessageList(message);
        if (messageList.size() == 0){
            mymp = StrResponse.error("查询所有消息失败");
            return ERROR;
        }

        int countLike = messageService.queryCountLike(message);

        PageResult pageResult = new PageResult();

        pageResult.setCurrentPage(message.getPageResult().getCurrentPage());
        pageResult.setPageSize(message.getPageResult().getPageSize());
        pageResult.setTotalSize(countLike);
        pageResult.setItems(messageList);

        mymp = StrResponse.success("查询消息成功", pageResult);
        return SUCCESS;
    }

    public String selectMessageNumber(){
        message = JSON.parseObject(StrResponse.getStrResponse(), Message.class);

        if(StringUtils.isEmpty(message.getReceivedId())){
            mymp = StrResponse.error("用户ID不能为空");
            return ERROR;
        }

        if(StringUtils.isEmpty(message.getPreviewState())){
            mymp = StrResponse.error("消息标识不能为空");
            return ERROR;
        }

        messageService = new MessageServiceImpl();

        int count = messageService.newsArticleNumber(message);
        if (count == 0){
            mymp = StrResponse.error("查询消息失败");
            return ERROR;
        }

        mymp = StrResponse.success("查询消息成功", count);
        return SUCCESS;
    }
}
